home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / SpriteWorld 2.2 Extra Demos / Scaling Blitter Test / Simple.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-08  |  10.2 KB  |  356 lines  |  [TEXT/CWIE]

  1. ///--------------------------------------------------------------------------------------
  2. // Simple.c
  3. ///--------------------------------------------------------------------------------------
  4.  
  5. #include <SWIncludes.h>            // Automatically include all SpriteWorld headers
  6. #include <SWGameUtils.h>
  7. #include <SWFPSReport.h>
  8. #include <BlitPixieScaled.h>
  9.  
  10. #include "SWApplication.h"
  11. #include "Simple.h"
  12.  
  13.  
  14. #define kFullScreenWindow        false        // Set to true if you want a window larger than 640x480 
  15. #define kInterlacedMode            false        // You can use interlacing even with non-scrolling animations
  16. #define kSyncToVBL                false        // Syncs the animation to the VBL
  17. #define kWorldRectInset            0            // Makes the SpriteWorld smaller than the window.
  18. #define kMaxFPS                    30            // Keep the animation from going too fast
  19.  
  20. #define kDoBouncingBallTest        false        // switches between two types of tests
  21. #define kMakeIdleSprite            true        // set to true to test scaled idle sprite overlap
  22. #define kNumSprites                8            // the number of sprites, including the idle sprite
  23. #define kMaxSpriteMoveDelta        8            // maximum speed the sprites can move
  24. #define kMinSpriteSize            30            // the minimum size of a scaled sprite
  25. #define kMaxSpriteSize            210            // the maximum size of a scaled sprite
  26.  
  27.  
  28. SpriteWorldPtr        gSpriteWorldP;
  29. SpriteLayerPtr        gSpriteLayerP;
  30. SpritePtr            gMasterBallSpriteP;
  31. WindowPtr            gWindowP;
  32.  
  33.  
  34. ///--------------------------------------------------------------------------------------
  35. // main
  36. ///--------------------------------------------------------------------------------------
  37.  
  38. void main(void)
  39. {
  40.     Initialize(kNumberOfMoreMastersCalls);
  41.     Randomize();
  42.  
  43.     if (SWHasSystem7())
  44.     {
  45.         CreateWindow();
  46.         SetUpSpriteWorld();
  47.         CreateBallSprite();
  48.         
  49.         AddSprites();
  50.         RunAnimation();
  51.         CleanUp();
  52.     }
  53.     else
  54.     {
  55.         CantRunOnThisMachine();
  56.     }
  57. }
  58.  
  59.  
  60. ///--------------------------------------------------------------------------------------
  61. //    CreateWindow
  62. ///--------------------------------------------------------------------------------------
  63.  
  64. void    CreateWindow(void)
  65. {
  66.     Rect        windRect;
  67.     RgnHandle    mBarUpdateRgn;
  68.     short        depth;
  69.     
  70.     gWindowP = GetNewCWindow(kWindowResID, NULL, (WindowPtr)-1L);
  71.  
  72.     if (gWindowP != NULL)
  73.     {
  74.         depth = GetDepthFromWindow(gWindowP);
  75.         if ( depth < 8 || depth > 16 )
  76.         {
  77.             SetCursor(&qd.arrow);
  78.             StopAlert(130, NULL);
  79.             ExitToShell();
  80.         }
  81.         
  82.         if (kFullScreenWindow == true)
  83.         {
  84.             SizeWindow(gWindowP, qd.screenBits.bounds.right, 
  85.                             qd.screenBits.bounds.bottom, false);
  86.             MoveWindow(gWindowP, 0, 0, false);
  87.         }
  88.         else
  89.         {
  90.             MoveWindow(gWindowP, 0, 0, false);
  91.             windRect = gWindowP->portRect;
  92.             
  93.                 // Make sure the window fits on the screen
  94.             if (windRect.bottom > qd.screenBits.bounds.bottom ||
  95.                  windRect.right > qd.screenBits.bounds.right)
  96.             {
  97.                     // Make it smaller so it fits on the screen
  98.                 SizeWindow(gWindowP, qd.screenBits.bounds.right, 
  99.                                 qd.screenBits.bounds.bottom, false);
  100.                 MoveWindow(gWindowP, 0, 0, false);
  101.             }
  102.             else
  103.             {
  104.                     // Center window in screen
  105.                 CenterRect(&windRect, &qd.screenBits.bounds);
  106.                 MoveWindow(gWindowP, windRect.left, windRect.top, false);
  107.             }
  108.         }
  109.         
  110.         ShowWindow(gWindowP);
  111.         SetPort(gWindowP);
  112.         mBarUpdateRgn = SWHideMenuBar(gWindowP); // Must be done *after* showing window!
  113.         EraseRgn(mBarUpdateRgn);
  114.     }
  115.     else
  116.         CantFindResource();
  117. }
  118.  
  119.  
  120. ///--------------------------------------------------------------------------------------
  121. //    SetUpSpriteWorld
  122. ///--------------------------------------------------------------------------------------
  123.  
  124. void    SetUpSpriteWorld(void)
  125. {
  126.     OSErr            err;
  127.     Rect            worldRect;
  128.     PixPatHandle    pixPatH;
  129.  
  130.     SetCursor(*GetCursor(watchCursor));
  131.  
  132.         // Initialize the SpriteWorld package
  133.     err = SWEnterSpriteWorld();
  134.     FatalError(err);
  135.     
  136.     worldRect = gWindowP->portRect;
  137.     InsetRect(&worldRect, kWorldRectInset, kWorldRectInset);
  138.     
  139.         // Create the SpriteWorld
  140.     err = SWCreateSpriteWorldFromWindow(&gSpriteWorldP, (CWindowPtr)gWindowP, 
  141.         &worldRect, NULL, 0);
  142.     FatalError(err);
  143.  
  144.         // Create the Sprite Layer
  145.     err = SWCreateSpriteLayer(&gSpriteLayerP);
  146.     FatalError(err);
  147.     
  148.         // Put the pieces together and lock the SpriteWorld. 
  149.         // Note: since we are locking the SpriteWorld before adding the sprites,
  150.         // we must make sure to lock each sprite individually when creating them.
  151.     SWAddSpriteLayer(gSpriteWorldP, gSpriteLayerP);
  152.     SWLockSpriteWorld(gSpriteWorldP);
  153.     
  154.     
  155.         // Use BlitPixie for the screen and offscreen DrawProcs.
  156.     if (gSpriteWorldP->pixelDepth == 8)        // 256 colors
  157.     {
  158.         if (kInterlacedMode)
  159.         {
  160.             SWSetSpriteWorldScreenDrawProc(gSpriteWorldP, BP8BitInterlacedRectDrawProc);
  161.             SWSetSpriteWorldOffscreenDrawProc(gSpriteWorldP, BP8BitInterlacedRectDrawProc);
  162.         }
  163.         else
  164.         {
  165.             SWSetSpriteWorldScreenDrawProc(gSpriteWorldP, BlitPixie8BitRectDrawProc);
  166.             SWSetSpriteWorldOffscreenDrawProc(gSpriteWorldP, BlitPixie8BitRectDrawProc);
  167.         }
  168.     }
  169.     else if ( !(SW_PPC && gSpriteWorldP->pixelDepth < 8) )    // not 256 colors
  170.     {
  171.         if (kInterlacedMode)
  172.         {
  173.             SWSetSpriteWorldScreenDrawProc(gSpriteWorldP, BPAllBitInterlacedRectDrawProc);
  174.             SWSetSpriteWorldOffscreenDrawProc(gSpriteWorldP, BPAllBitInterlacedRectDrawProc);
  175.         }
  176.         else
  177.         {
  178.             SWSetSpriteWorldScreenDrawProc(gSpriteWorldP, BlitPixieAllBitRectDrawProc);
  179.             SWSetSpriteWorldOffscreenDrawProc(gSpriteWorldP, BlitPixieAllBitRectDrawProc);
  180.         }
  181.     }
  182.  
  183.         // Draw the background
  184.     SWSetPortToBackground(gSpriteWorldP);
  185.     
  186.     if (gSpriteWorldP->pixelDepth == 1)
  187.         pixPatH = GetPixPat(129);        // B&W dithered background
  188.     else
  189.         pixPatH = GetPixPat(128);        // Color
  190.     
  191.     if (pixPatH != NULL)
  192.     {
  193.         FillCRect(&gSpriteWorldP->backRect, pixPatH);
  194.         DisposePixPat(pixPatH);
  195.     }
  196.     
  197.         // Set up other miscellaneous options
  198.     SWSetSpriteWorldMaxFPS(gSpriteWorldP, kMaxFPS);
  199.     SWSyncSpriteWorldToVBL(gSpriteWorldP, kSyncToVBL);
  200.     SWSetCleanUpSpriteWorld(gSpriteWorldP);
  201. }
  202.  
  203.  
  204. ///--------------------------------------------------------------------------------------
  205. //    CreateBallSprite - load the master ball sprite and prepare it for cloning later
  206. ///--------------------------------------------------------------------------------------
  207.  
  208. void    CreateBallSprite(void)
  209. {
  210.     OSErr    err;
  211.     
  212.     err = SWCreateSpriteFromPictResource(gSpriteWorldP, &gMasterBallSpriteP, NULL, 
  213.             128, 128, 1, kFatMask);
  214.     FatalError(err);
  215.  
  216.         // Set up the sprite (remember that it must be locked!)
  217.     SWLockSprite(gMasterBallSpriteP);
  218.     SWSetSpriteMoveBounds(gMasterBallSpriteP, &gSpriteWorldP->backRect);
  219.     SWSetSpriteMoveProc(gMasterBallSpriteP, BallSpriteMoveProc);
  220. }
  221.  
  222.  
  223. ///--------------------------------------------------------------------------------------
  224. //    AddSprites - clone the master sprite, and add the clones to the SpriteWorld
  225. ///--------------------------------------------------------------------------------------
  226.  
  227. void    AddSprites(void)
  228. {
  229.     SpritePtr    newSpriteP;
  230.     short        spriteNum;
  231.     OSErr        err;
  232.     
  233.     for (spriteNum = 0; spriteNum < kNumSprites; spriteNum++)
  234.     {
  235.             // Clone the master sprite. The clone doesn't need to be locked,
  236.             // since the master sprite was already locked.
  237.         err = SWCloneSprite(gMasterBallSpriteP, &newSpriteP, NULL);
  238.         FatalError(err);
  239.         
  240.         SWAddSprite(gSpriteLayerP, newSpriteP);
  241.         
  242.         if (kDoBouncingBallTest)
  243.         {
  244.             SWSetSpriteScaledSize(newSpriteP, spriteNum*20 + 20, 
  245.                     spriteNum*20 + 20);
  246.             
  247.             SWSetSpriteLocation(newSpriteP,
  248.                     (spriteNum+1) * 50, 
  249.                     (spriteNum+2) * 50);
  250.                     
  251.             SWSetSpriteMoveDelta(newSpriteP, spriteNum+1, spriteNum + 1);
  252.         }
  253.         else
  254.         {
  255.             SWSetSpriteScaledSize(newSpriteP, GetRandom(kMinSpriteSize,kMaxSpriteSize), 
  256.                     GetRandom(kMinSpriteSize,kMaxSpriteSize) );
  257.             
  258.             SWSetSpriteLocation(newSpriteP,
  259.                     GetRandom(0, gSpriteWorldP->backRect.right-44), 
  260.                     GetRandom(0, gSpriteWorldP->backRect.bottom-44) );
  261.                     
  262.             
  263.             do
  264.             {
  265.                 SWSetSpriteMoveDelta(newSpriteP,
  266.                         GetRandom(-kMaxSpriteMoveDelta, kMaxSpriteMoveDelta), 
  267.                         GetRandom(-kMaxSpriteMoveDelta, kMaxSpriteMoveDelta));
  268.             } while (newSpriteP->horizMoveDelta == 0 || newSpriteP->vertMoveDelta == 0);
  269.         }
  270.         
  271.         
  272.             // Make this one a large idle sprite
  273.         if (kMakeIdleSprite && spriteNum == 0)
  274.         {
  275.             SWSetSpriteScaledSize(newSpriteP, 500, 400 );
  276.             SWSetSpriteLocation(newSpriteP, 50, 20);
  277.             SWSetSpriteMoveDelta(newSpriteP, 0, 0);
  278.             SWSetSpriteMoveProc(newSpriteP, NULL);
  279.         }
  280.     }
  281. }
  282.  
  283.  
  284. ///--------------------------------------------------------------------------------------
  285. //    RunAnimation
  286. ///--------------------------------------------------------------------------------------
  287.  
  288. void    RunAnimation(void)
  289. {
  290.     unsigned long    frames;
  291.     
  292.     SetCursor(&qd.arrow);
  293.     HideCursor();
  294.     
  295.         // Make sure CopyBits, if used, doesn't try to colorize things
  296.     SWSetPortToWindow(gSpriteWorldP);
  297.     ForeColor(blackColor);
  298.     BackColor(whiteColor);
  299.     
  300.     SWUpdateSpriteWorld(gSpriteWorldP, true);
  301.     
  302.     FatalError( SWStickyError() ); // Make sure no errors got past us during setup
  303.     
  304.     frames = 0;
  305.     StartTimer();    // For FPS report later
  306.     
  307.     while (!Button())
  308.     {
  309.         SWProcessSpriteWorld(gSpriteWorldP);
  310.         FatalError( SWStickyError() );    // Make sure no errors occurred during a MoveProc, etc.
  311.         SWAnimateSpriteWorld(gSpriteWorldP);
  312.         
  313.         if (gSpriteWorldP->frameHasOccurred)
  314.             frames++;        
  315.     }
  316.     
  317.     SWShowMenuBar((WindowPtr)gWindowP);
  318.     ShowResults(frames);    // Show FPS report
  319. }
  320.  
  321.  
  322. ///--------------------------------------------------------------------------------------
  323. //     CleanUp - This function is not really necessary, since the system will dispose
  324. //    everything automatically when the program quits. However, if you sync the animation
  325. //    to the VBL, you must either call SWDisposeSpriteWorld, or SWSyncSpriteWorldToVBL
  326. //    with a value of false before your program quits, in order to remove the VBL task.
  327. ///--------------------------------------------------------------------------------------
  328.  
  329. void    CleanUp(void)
  330. {
  331.         // Dispose the master sprite, since it isn't included in the SpriteWorld
  332.     SWDisposeSprite(&gMasterBallSpriteP);
  333.     
  334.         // Dispose the SpriteWorld, including all sprites and layers currently in it
  335.     SWDisposeSpriteWorld(&gSpriteWorldP);
  336.     SWExitSpriteWorld();
  337.     
  338.     FlushEvents(everyEvent, 0);
  339.     ShowCursor();
  340. }
  341.  
  342.  
  343. ///--------------------------------------------------------------------------------------
  344. //  BallSpriteMoveProc
  345. ///--------------------------------------------------------------------------------------
  346.  
  347. SW_FUNC void BallSpriteMoveProc(SpritePtr ballSpriteP)
  348. {    
  349.     SWOffsetSprite(ballSpriteP, ballSpriteP->horizMoveDelta, ballSpriteP->vertMoveDelta);
  350.     
  351.     if (kDoBouncingBallTest)
  352.         (void)SWBounceSprite(ballSpriteP);
  353.     else
  354.         (void)SWWrapSprite(ballSpriteP);
  355. }
  356.